package com.leyou.search.mq;

import com.leyou.common.constants.MQConstants.*;
import com.leyou.search.service.SearchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class ItemListener {

    @Autowired
    SearchService searchService;

    //下架
    //durable持久性
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = QueueConstants.SEARCH_ITEM_DOWN,durable = "true"),
            exchange = @Exchange(name = ExchangeConstants.ITEM_EXCHANGE_NAME,type = ExchangeTypes.TOPIC),
            key = RoutingKeyConstants.ITEM_DOWN_KEY
    ))
    public void listenerItemDown(Long id){
        if (id!=null&&id>0){
            searchService.deleteGoodsById(id);
            log.info("--------->listenerItemDown");
        }
    }

    //上架
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = QueueConstants.SEARCH_ITEM_UP,durable = "true"),
            exchange = @Exchange(name = ExchangeConstants.ITEM_EXCHANGE_NAME,type = ExchangeTypes.TOPIC),
            key = RoutingKeyConstants.ITEM_UP_KEY
    ))
    public void listenerItemUp(Long id){
        if (id!=null&&id>0){
            searchService.saveGoodsById(id);
            log.info("--------->listenerItemUp");
        }
    }
}
